<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
      label-position="left">
      <el-card class="box-card">
        <div class="title-container">
          <h3 class="title">⾼校勤⼯俭学管理系统</h3>
        </div>
        <el-form-item prop="phone">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="phone" v-model="loginForm.phone" placeholder="phone" name="phone" type="text" tabindex="1"
            auto-complete="on" />
        </el-form-item>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
            placeholder="Password" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <div class="tips">
          <span style="margin-right:20px;">管理员: admin 学⽣: student</span>
          <span> 密码: 123456</span>
        </div>
        <div style="display: flex">
          <el-button type="info" style="width:100%;margin-bottom:30px;" @click="registerDialog = true">注册</el-button>
          <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
            @click.native.prevent="handleLogin">登录</el-button>
          <el-button type="primary" style="width:100%;margin-bottom:30px;" @click="getStudent">新用户注册后点击</el-button>
        </div>




      </el-card>
    </el-form>
    <el-dialog title="快速注册" :visible.sync="registerDialog">
      <el-form :model="registerFrom">
        <el-form-item label="手机号">
          <el-input v-model="registerFrom.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="registerFrom.password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="registerDialog = false">取 消</el-button>
        <el-button type="primary" @click="registerUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { validUsername } from '@/utils/validate'
import jobApi from '@/api/job'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        phone: '',
        password: ''
      },
      loginRules: {
        phone: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      registerFrom: {
        phone: "",
        password: ""
      },
      stuForm: {
        // studentId: "",
        name: "",
        phone: "",
        age: 0,
        college: "string",
        gender: "string",
        isDelete: false,
        jobName: "string",
        major: "string",
        operation: "string",
        qingongjianxueStatus: "string",
        stuclass: "string",
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      registerDialog: false,
      phone: ''
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          localStorage.setItem("userPhone",this.loginForm.phone)
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch((err) => {
            console.log(err)
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })

    },
    registerUpdate() {
      this.phone = this.registerFrom.phone
      // console.log("this.phone",this.phone)
      jobApi.register(this.registerFrom)
        .then((res) => {
          this.$message({
            type: 'success',
            message: '注册成功!'
          })
          // localStorage.setItem("studentPhone",this.registerFrom.phone)
          // console.log("当前表单内的手机号，传递给查询接口", this.registerFrom.phone)
          console.log(res)
        }).catch((err) => {
          this.$message({
            type: 'error',
            message: '注册失败!'
          })
          console.log(err)
        })
      this.registerDialog = false
      // this.registerFrom = {}
      // jobApi.getStuInfo(this.phone.toString()).then((res) => {
      //   // this.stuForm = res.data
      //   console.log("根据手机号查询到的个人信息", res)
      // })
    },
    getStudent() {
      console.log(this.phone)
      jobApi.getStuInfo(this.phone).then((res) => {
        this.stuForm.name = res.data.stu[0].name
        this.stuForm.phone = res.data.stu[0].phone
        // this.stuForm.studentId = res.data.stu[0].id
        this.$message({
          type: 'success',
          message: '已同步数据!'
        })
        console.log("根据手机号查询到的个人信息", this.stuForm)
      })
      jobApi.addStudent(this.stuForm)
        .then(res => {
          this.$message.success('添加成功');

        });
    }
  }
}
</script>
<style lang="scss">
/* 修复input 背景不协调 和光标变⾊ */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
$bg: #283443;
$light_gray: #000000;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>
<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #525252;

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  background-image: url("../../assets/404_images/cool-background.png");
  overflow: hidden;

  .login-form {
    position: relative;
    // display: flex;
    // justify-content: center;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    // color: #fff;
    color: #909399;

    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>